﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using PJ.Business;
using Microsoft.Practices.Composite.Events;
using PJ.Business.SalePurchase.Sale;
using PJ.Business.Partner;
using PJ.Data;

namespace PJ.Presentation.Partner
{
    public class CustomerSearchModel : SearchModel, ICustomerSearchModel
    {
        public CustomerSearchModel()
            : base(null, null, null)
        {
        }
        [Microsoft.Practices.Unity.InjectionConstructor]
        public CustomerSearchModel(ICustomerSearchView view, IEventAggregator aggregator, ICustomerService service)
            : base(view, aggregator, service)
        {
        }




        public override void OnSearch(object sender)
        {
            string[] result;
            try
            {
                if (SearchText == null || SearchText == "")
                    result = null;
                else
                    result = Service.Search(SearchText, GetDataMode.UseCache);
            }
            catch (Exception e)
            {
                result = null; ;
                string message = "";
                Exception inner = e;
                do
                {
                    message += e.Message + "\n";
                    message += "Stack : " + e.StackTrace + "\n";
                }
                while ((inner = inner.InnerException) != null);
                MessageBox.Show(message);
            }
            EventAggregator.GetEvent<CustomerSearchResultChangeEvent>().Publish(result);
        }
    }
}
